package com.derbysoft.dswitch.adapter.ihg.web.action.admin.setting.hotel;

import com.derbysoft.dswitch.adapter.ihg.common.domain.HotelMapping;
import com.derbysoft.dswitch.adapter.ihg.common.domain.HotelStatus;
import com.derbysoft.dswitch.adapter.ihg.common.repository.HotelRepository;
import com.derbysoft.dswitch.adapter.ihg.web.action.support.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddAction extends BaseAction {

    @Autowired
    private HotelRepository hotelRepository;

    private HotelMapping hotelMapping;

    public static final String IMPORT_VIEW = "import";

    private static final int MAX_LINES = 500;

    private String hotels;

    private String result;

    @Action("update")
    public String update() {
        if (!StringUtils.isBlank(hotelMapping.getCooperatorCode())
                && !StringUtils.isBlank(hotelMapping.getDerbyCode())
                && hotelMapping.getStatus() != null) {
            HotelMapping oldHotelMapping = hotelRepository.load(hotelMapping.getId());
            if (oldHotelMapping != null) {
                oldHotelMapping.setDerbyCode(hotelMapping.getDerbyCode());
                oldHotelMapping.setHotelName(hotelMapping.getHotelName());
                oldHotelMapping.setStatus(hotelMapping.getStatus());

                hotelRepository.save(oldHotelMapping);
            }
        }
        return BaseAction.LIST;
    }

    @Action("add")
    public String add() {
        if (!StringUtils.isBlank(hotelMapping.getCooperatorCode())
                && !StringUtils.isBlank(hotelMapping.getDerbyCode())
                && hotelMapping.getStatus() != null) {
            if (hotelRepository.loadByHotelCode(hotelMapping.getCooperatorCode()) != null) {
                ((HttpServletRequest) ActionContext.getContext()
                        .get(ServletActionContext.HTTP_REQUEST))
                        .setAttribute("cooperatorCode_error", hotelMapping.getCooperatorCode() + " : had already exists !!");
                return view();
            }
            if (hotelRepository.loadByDerbyCode(hotelMapping.getDerbyCode()) != null) {
                ((HttpServletRequest) ActionContext.getContext()
                        .get(ServletActionContext.HTTP_REQUEST))
                        .setAttribute("derbyCode_error", hotelMapping.getDerbyCode() + " : had already exists !!");
                return view();
            }
            hotelRepository.save(hotelMapping);
        }
        return BaseAction.LIST;
    }

    @Action("view")
    public String view() {
        return ADD;
    }

    @Action("edit")
    public String edit() {
        if (hotelMapping != null && hotelMapping.getId() != null) {
            hotelMapping = hotelRepository.load(hotelMapping.getId());
        }
        return EDIT;
    }

    @Action("importView")
    public String importView() {
        return IMPORT_VIEW;
    }

    @Action("batchImport")
    public String batchImport() {
        if (StringUtils.isBlank(hotels)) {
            result = "invalid lines";
            return IMPORT_VIEW;
        }

        Map<String, HotelMapping> mappingMap = new HashMap<String, HotelMapping>();
        String[] lines = hotels.replace("\r\n", "\n").split("\n");
        for (int i = 0; i < lines.length && i < MAX_LINES; i++) {
            String[] columns = lines[i].split("\\|");
            if (columns == null || columns.length != 4) {
                continue;
            }
            if (hotelRepository.loadByHotelCode(columns[0]) != null) {
                continue;
            }
            HotelMapping hotelMap = new HotelMapping();
            hotelMap.setCooperatorCode(columns[0].trim());
            hotelMap.setDerbyCode(columns[1].trim());
            hotelMap.setHotelName(columns[2].trim());
            hotelMap.setStatus(columns[3].trim().toUpperCase().equals("ENABLED") ? HotelStatus.ENABLED : HotelStatus.DISABLED);
            mappingMap.put(columns[0].trim() + "_" + columns[1].trim(), hotelMap);
        }
        List<HotelMapping> hotelMappings = new ArrayList<HotelMapping>(mappingMap.values());
        StringBuilder builder = new StringBuilder();
        for (HotelMapping mapping : hotelMappings) {
            builder.append("[").append(mapping.getCooperatorCode()).append("]").append(" OK.").append("<br>");
        }
        result = builder.toString();
        hotelRepository.saveAll(hotelMappings);
        return IMPORT_VIEW;
    }

    public String getHotels() {
        return hotels;
    }

    public void setHotels(String hotels) {
        this.hotels = hotels;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public HotelMapping getHotelMapping() {
        return hotelMapping;
    }

    public void setHotelMapping(HotelMapping hotelMapping) {
        this.hotelMapping = hotelMapping;
    }
}
